package cn.mumzone.pos.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.loading.button.LoadingButton;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;

/**
 * Created by qiuzhenhuan on 2019/7/1.
 */

public class PosAlertDialog extends AlertDialog {


    @BindView(R.id.btn_secondary)
    Button btnSecondary;
    @BindView(R.id.btn_primary)
    LoadingButton btnPrimary;
    @BindView(R.id.tv_remind)
    TextView tvRemind;


    private Context context;
    private String primaryText;
    private String secondaryText;
    private String remindText;
    private String primaryLoadingText;
    private boolean useLoadingButton;
    private boolean mCancelable = true;
    private OnConfirmClickListener onConfirmClickListener;
    private OnCancelClickListener onCancelClickListener;

    private PosAlertDialog(Builder builder) {
        super(builder.context, cn.mumzone.basic.R.style.dialog);
        this.context = builder.context;
        this.primaryText = builder.primaryText;
        this.secondaryText = builder.secondaryText;
        this.remindText = builder.remindText;
        this.onConfirmClickListener = builder.onConfirmClickListener;
        this.onCancelClickListener = builder.onCancelClickListener;
        this.useLoadingButton = builder.useLoadingButton;
        this.primaryLoadingText = builder.primaryLoadingText;
        this.mCancelable = builder.cancelable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(context, R.layout.dialog_pos_alert, null);
        setContentView(view);
        ButterKnife.bind(this);

        setCanceledOnTouchOutside(mCancelable);

        btnPrimary.setText(StringUtils.isEmpty(primaryText) ? "确定" : primaryText);
        btnSecondary.setText(StringUtils.isEmpty(secondaryText) ? "取消" : secondaryText);
        tvRemind.setText(StringUtils.get(remindText));
        btnPrimary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PosAlertDialog.this.onConfirmClickListener != null) {
                    PosAlertDialog.this.onConfirmClickListener.onConfirmClick(v, PosAlertDialog.this);
                }
                if (useLoadingButton) {
                    btnPrimary.startLoading(StringUtils.isEmpty(primaryLoadingText) ?
                            StringUtils.isEmpty(primaryText) ? "确定" : primaryText :
                            primaryLoadingText, btnSecondary);
                } else {
                    dismiss();
                }
            }
        });

        btnSecondary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCancelClickListener != null) {
                    onCancelClickListener.onCancelClick(v, PosAlertDialog.this);
                } else {
                    dismiss();
                }
            }
        });
    }

    @Override
    public void show() {
        super.show();

        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = 382;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(layoutParams);
    }

    public void setRemindText(String remindText) {
        this.remindText = remindText;
        tvRemind.setText(remindText);
    }


    public void cancelLoading() {
        btnPrimary.stopLoading(primaryText);
        dismiss();
    }

    public static class Builder {
        private Context context;
        private String primaryText;
        private String secondaryText;
        private String remindText;
        private boolean useLoadingButton;
        private String primaryLoadingText;
        private boolean cancelable;
        private OnConfirmClickListener onConfirmClickListener;
        private OnCancelClickListener onCancelClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setCanceledOnTouchOutside(boolean cancel) {
            this.cancelable = cancel;
            return this;
        }

        public Builder setPrimaryText(String primaryText) {
            this.primaryText = primaryText;
            return this;
        }

        public Builder setSecondaryText(String secondaryText) {
            this.secondaryText = secondaryText;
            return this;
        }

        public Builder setRemindText(String remindText) {
            this.remindText = remindText;
            return this;
        }

        public Builder setPrimaryLoadingText(String primaryLoadingText) {
            this.primaryLoadingText = primaryLoadingText;
            return this;
        }

        public Builder setUseLoadingButton(boolean useLoadingButton) {
            this.useLoadingButton = useLoadingButton;
            return this;
        }

        public Builder setOnConfirmClickListener(OnConfirmClickListener onConfirmClickListener) {
            this.onConfirmClickListener = onConfirmClickListener;
            return this;
        }

        public Builder setOnCancelClickListener(OnCancelClickListener onCancelClickListener) {
            this.onCancelClickListener = onCancelClickListener;
            return this;
        }

        public PosAlertDialog build() {
            return new PosAlertDialog(this);
        }
    }

    public interface OnConfirmClickListener {

        void onConfirmClick(View view, Dialog dialog);
    }

    public interface OnCancelClickListener {

        void onCancelClick(View view, Dialog dialog);
    }
}
